const store = {
	state: {
		key: 'token',
		userKey: 'userinfo',
		token: '',
		userinfo: {}
	},
	mutations: {
		//设置key值
		setKey(state, key) {
			state.key = key
		},
		//设置userKey值
		setUserKey(state, userKey) {
			state.userKey = userKey
		},
		//存储token
		saveToken: (state, token) => {
			state.token = token
			uni.setStorageSync(state.key, token)
		},
		//删除token
		clearToken: state => {
			state.token = ''
			uni.removeStorageSync(state.key)
		},
		//存储用户信息
		saveUserinfo: (state, userinfo) => {
			state.userinfo = userinfo
			uni.setStorageSync(state.userKey, userinfo)
		},
		//删除用户信息
		clearUserinfo: state => {
			state.userinfo = {}
			uni.removeStorageSync(state.userKey)
		},
		//更新token
		refreshToken({
			state
		}) {
			state.token = uni.getStorageSync(state.key)
		},
		//更新userinfo
		refreshUserinfo({
			state
		}) {
			state.userinfo = uni.getStorageSync(state.userKey)
		}
	},
	actions: {

	},
	getters: {
		//登录状态
		loginStatus: state => {
			return state.token || uni.getStorageSync(state.key) ? true : false
		},
		//token
		token: state => {
			return state.token || uni.getStorageSync(state.key)
		},
		//用户信息
		userinfo: state => {
			return Object.keys(state.userinfo).length ? state.userinfo : uni.getStorageSync(state
				.userKey) || {}
		},
	}
}

export default store
